public class DAY0425 {
    //剑指 Offer 59 - I. 滑动窗口的最大值

    //剑指 Offer 59 - II. 队列的最大值
    class MaxQueue {
        int[] nums = new int[20000];
        int begin = 0 ;
        int end = 0 ;

        public MaxQueue() {}

        public int max_value() {
            int ans = -1 ;
            for (int i = begin; i != end; ++i) {
                ans = Math.max(ans , nums[i]);
            }
            return ans;
        }

        public void push_back(int value) {
            nums[end++] = value ;
        }

        public int pop_front() {
            if (begin == end){
                return -1;
            }
            return nums[begin++];
        }
    }

    //剑指 Offer 29. 顺时针打印矩阵
    public int[] spiralOrder(int[][] matrix) {
        //首先判断是否为空
        if (matrix == null || matrix.length ==0 || matrix[0].length == 0){
            return new int[0];
        }
        //新建一个大小和原数组大小相同的数组
        int rows = matrix.length ;
        int columns = matrix[0].length ;
        boolean[][] nums = new boolean[rows][columns] ;
        //开始判断
        int total = rows * columns ;
        int[] order = new int[total];
        int row = 0, column = 0;
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int directionIndex = 0;
        for (int i = 0; i < total; i++) {
            order[i] = matrix[row][column];
            nums[row][column] = true;
            int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
            if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || nums[nextRow][nextColumn]) {
                directionIndex = (directionIndex + 1) % 4;
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return order;
    }
}
